$(function(){
    //需求1：获取
    getUserInfo()


    // 需求2 ：退出
    $('#logout').on('click',function(){
        console.log(1);
        layer.confirm('确定退出吗?', {icon: 3, title:'提示'}, function(index){
            //do something
            localStorage.removeItem('token')
            location.href='/login.html'
            layer.close(index);
          });
    })
})
  //获取用户信息渲染头像封装成函数
  function getUserInfo(){
    axios({
        url:'/my/userinfo',
        // headers:{
        //     Authorization:localStorage.getItem('token')
        // }
    }).then(({data:res})=>{
        // console.log(res);
        //判断
        if(res.status !=0){
            return layui.layer.msg(res.message)
        }
        //渲染
        renderAvatar(res.data)
    })
  }
  //渲染用户信息和头像
  function renderAvatar(user){
    let name = user.nickname ||user.username
    $('.welcome').html('欢迎&nbsp'+name)
    if(user.user_pic !==null){
        $('.avarat-text').hide()
        $('.layui-nav-img').show().attr('src',user.user_pic)
    } else{
        $('.layui-nav-img').hide()
        let first = name[0].toUpperCase()
        $('.avatar-text').show().html(first)
    }
  }